package com.kgc.smbms.interceptor;

import com.kgc.smbms.bean.User;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created On : 13/9/2022.
 * <p>
 * Author : huayu
 * <p>
 * Description: checkLoginInterceptor 检查用户是否登录拦截器
 */
@Component
public class checkLoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        User loginUser = (User)request.getSession().getAttribute("loginUser");

        if(loginUser != null){
            return true;
        }
        System.out.println("checkLoginInterceptor ==》用户未登录！！！");

        response.sendRedirect(request.getContextPath()+"/login.jsp");

        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
